******************
* Loading dataset*
******************

use "/Users/damienbol/Documents/Recherche/Peer-reviewed articles/strategic entry lab/BD2.dta", replace

* Computing extra variables by subjects
***************************************

*Entry (lagged)
sort session_id subject period
by session_id subject : gen lag_entry_subject = entry[_n-1]

*Cumulative frequence of entries since the beginning of the experiment (lagged)
bysort session_id subject (period) : gen cum_entry_subject=sum(entry)
bysort session_id subject period : gen freq_entry_subject=cum_entry_subject/period
by session_id subject : gen lag_freq_entry_subject = freq_entry_subject[_n-1]

*Cumulative frequence of wins since the beginning of the experiment (lagged)
bysort session_id subject (period) : gen cum_win_subject=sum(win)
bysort session_id subject period : gen freq_win_subject=cum_win_subject/period
by session_id subject : gen lag_freq_win_subject = freq_win_subject[_n-1]

*Cumulative frequence of qualifications since the beginning of the experiment (lagged) - For majority runoff only 
bysort session_id subject (period) : gen cum_qual_subject=sum(qual) if maj==1
bysort session_id subject period : gen freq_qual_subject=cum_qual_subject/period
by session_id subject : gen lag_freq_qual_subject = freq_qual_subject[_n-1]

* Computing extra variables by position
***************************************

*Entry (lagged)
sort session_id position period
by session_id position : gen lag_entry_position = entry[_n-1]

*Cumulative frequence of entries since the beginning of the experiment (lagged)
bysort session_id position (period) : gen cum_entry_position=sum(entry)
bysort session_id position period : gen freq_entry_position=cum_entry_position/period
by session_id position : gen lag_freq_entry_position = freq_entry_position[_n-1]

*Cumulative frequence of wins since the beginning of the experiment (lagged)
bysort session_id position (period) : gen cum_win_position=sum(win)
bysort session_id position period : gen freq_win_position=cum_win_position/period
by session_id position : gen lag_freq_win_position = freq_win_position[_n-1]

*Cumulative frequence of qualifications since the beginning of the experiment (lagged, for majority runoff only)
bysort session_id position (period) : gen cum_qual_position=sum(qual) if maj==1
bysort session_id position period : gen freq_qual_position=cum_qual_position/period
by session_id position : gen lag_freq_qual_position = freq_qual_position[_n-1]

* Computing extra variables for the entire group
************************************************

*Entries (lagged)
bysort election_id : gen cum_entry=sum(entry)
bysort election_id : egen number_entry=max(cum_entry)
sort session_id group (period)
by session_id group : gen lag_number_entry = number_entry[_n-1]


* Analyses
**********

logit entry maj EntryOpt EntryOptPrev lag_freq_win_position i.position if serie!=1, or
logit entry maj i.maj##i.EntryOpt i.maj##i.EntryOptPrev i.maj##c.lag_freq_win_position i.position if serie!=1, or
logit entry maj EntryOpt EntryOptPrev lag_freq_win_position lag_freq_win_subject lag_freq_entry_subject risk i.position if serie!=1, or

* Graphs
*********

keep if group==1
graph twoway (line sumentry period_id if session==1)(line sumentry period_id if session==2)
graph twoway (line sumentry period_id if session==1)(line sumentry period_id if session==3)

graph twoway (line sumentry period_id if session==3)(line sumentry period_id if session==4)

tab sumentry period_id if session==3
bysort session: sum sumentry period_id 

